package tr.edu.boun.phys.springmassmodel.communication;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPSender {
	private int port;
	private InetAddress address;
	private DatagramSocket socket;
	private DatagramPacket packet;
	
	public UDPSender(int port) {
		this.port = port;
		
		try {
			address = InetAddress.getByName("localhost");
			socket = new DatagramSocket();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}
	
	public void send(int sample, int index) {
		String num1 = new Integer(sample).toString();
		String num2 = new Integer(index).toString();
		String message = new String(num1 + " " + num2 + " \n");
		/*
		 * sondaki newline PD'nin mesajin bittigini anlamasi icin...
		 */
		byte[] data = message.getBytes();
		packet = new DatagramPacket(data, data.length, address, port);
		try {
			socket.send(packet);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
